package com.jk.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.jk.dto.TbStudent;
import com.jk.pojo.PagedResponse;
import com.jk.pojo.ResponseResult;
import com.jk.service.TbStudentServiceFegin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * @author yzf
 * @version 1.0
 * @description: TODO
 * @date 2024/12/10 19:16
 */
@Api(tags = "学生模块")
@RestController
@RequestMapping(value = "student")
public class TbStudentController {

    @Autowired
    private TbStudentServiceFegin tbStudentServiceFegin;

    @ApiOperation(value = "查询",httpMethod = "POST",notes = "")
    @PostMapping(value = "findTbStudentList")
    public ResponseResult<PagedResponse<TbStudent>> findTbStudentList(@RequestBody TbStudent tbStudent, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize" ,defaultValue = "1") Integer pageSize){
        return new ResponseResult(200,"查询成功",tbStudentServiceFegin.findTbStudentList(tbStudent,pageNum,pageSize));
    }


    @ApiOperation(value = "删除",httpMethod = "DELETE",notes = "删除")
    @DeleteMapping("delTeacherById")
    public ResponseResult delTeacherById(@RequestParam Integer id) {

        try {
            tbStudentServiceFegin.deleteStudentById(id);
            return new ResponseResult<>(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"删除失败");

        }
    }


    @ApiOperation(value = "新增",httpMethod = "POST",notes = "新增")
    @RequestMapping("addStudent")
    public ResponseResult addStudent(@RequestBody TbStudent tbStudent) {
        try {
            //密码加密
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(tbStudent.getPassword());
            tbStudent.setPassword(encode);
            tbStudentServiceFegin.addStudent(tbStudent);
            return new ResponseResult<>(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"新增失败");
        }
    }

    @ApiOperation(value = "回显",httpMethod = "GET",notes = "回显")
    @RequestMapping("getStudentById")
    public ResponseResult<TbStudent> getTeacherById(@RequestParam Integer id) {
        return new ResponseResult<>(200,"回显成功", tbStudentServiceFegin.getStudentById(id));
    }

    /**
     * @description: 修改
     * @param: [sysUser]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author yzf
     * @date: 2024/11/5 20:22
     */

    @ApiOperation(value = "修改",httpMethod = "PUT",notes = "修改")
    @RequestMapping("upStudent")
    public ResponseResult upStudent(@RequestBody TbStudent tbStudent) {
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        tbAdmin.setModifyUser(loginUser.getUser().getUserName());
        tbStudent.setModifyTime(new Date());
        try {
            tbStudentServiceFegin.upStudent(tbStudent);
            return new ResponseResult<>(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult<>(500,"修改失败");

        }
    }

    @ApiOperation(value = "上下架",httpMethod = "POST",notes = "修改")
    @PostMapping("upstatus")
    public ResponseResult upstatus(@RequestBody TbStudent tbStudent) {
        try {
            tbStudentServiceFegin.upstatus(tbStudent);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    @ApiOperation(value = "上下架",httpMethod = "POST",notes = "修改")
    @PostMapping("updatestatus")
    public ResponseResult updatestatus(@RequestBody TbStudent tbStudent) {
        try {
            tbStudentServiceFegin.updatestatus(tbStudent);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }

    //导出
    @ApiOperation(value = "学生信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody TbStudent tbStudent, HttpServletResponse response){
        List<TbStudent> stuList = tbStudentServiceFegin.findTbStudent();
        //.xlsx格式
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("学生信息表");
            //设置标题
            params.setTitle("学生信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, TbStudent.class, stuList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
